package com.njlime.sys.service;

import com.njlime.sys.entities.Config;
import com.njlime.sys.entities.ConfigItem;
import com.njlime.sys.repository.ConfigDao;
import com.njlime.sys.repository.ConfigItemDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Dean on 2017-7-27.
 */
@Service
@Transactional(readOnly = true)
public class ConfigService {

    @Resource
    private ConfigDao configDao;
    @Resource
    private ConfigItemDao configItemDao;

    /**
     * 新增单位时默认添加配置项
     *
     * @param unit_id
     */
    @Transactional(readOnly = false)
    public void createByUnit(Integer unit_id) {
        List<Config> configList = configDao.list();
        if (configList != null && configList.size() > 0) {
            for (Config config : configList) {
                ConfigItem configItem = new ConfigItem();
                configItem.setConfig_id(config.getId());
                configItem.setConfig_name(config.getConfig_name());
                configItem.setConfig_value(config.getConfig_value());
                configItem.setUnit_id(unit_id);
                configItemDao.save(configItem);
            }
        }
    }

    /**
     * 删除
     * @param unit_id
     */
    @Transactional(readOnly = false)
    public void deleteByUnit(Integer unit_id){
        configItemDao.deleteByUnit(unit_id);
    }

    /**
     * 根据名称获取
     *
     * @param name
     * @param unit_id
     * @return
     */
    public ConfigItem getByName(String name, Integer unit_id) {
        return configItemDao.getByName(name, unit_id);
    }

    /**
     * 根据名称更新
     *
     * @param name
     * @param value
     * @param unit_id
     * @return
     */
    @Transactional(readOnly = false)
    public int updateByName(String name, String value, Integer unit_id) {
        return configItemDao.updateByName(name, value, unit_id);
    }

}
